package org.jivesoftware.webchat.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.infoview.db.DbUtil;

/**
 * 
 */
public class ValuationServlet extends HttpServlet implements Servlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ValuationServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		
        
        PrintWriter out= response.getWriter();
        response.setContentType("text/html;charset=gb2312");
        request.setCharacterEncoding("gb2312");
		
		//final String workgroup = request.getParameter("workgroup");
		//final String chatID = request.getParameter("chatID");
		//final String sessionID = request.getParameter("sessionID");
        
		String grade = request.getParameter("grade");
		String suggest = request.getParameter("suggest");
		String sessionID=request.getParameter("sessionID");
		
		
		System.out.println("grade="+grade);
		System.out.println("suggest="+suggest);
		System.out.println("sessionID="+sessionID);
		

		if (sessionID == null || "".equals(sessionID)) {
			String path=request.getContextPath();
			response.sendRedirect(path+"/chat-ended.jsp");
			return;
		}
		
		if (grade == null || "".equals(grade)) {
			grade = "1";
		}
		if (suggest == null) {
			suggest = "";
		}
		Connection conn=null;
	    PreparedStatement pstmt=null;
		try {
			conn = DbUtil.getConnection();

			String sql = "insert into session_info (sessionID, grade, suggestion) values (?,?,?)";
//			String sql = "insert into session_info (grade,suggestion) values(?,?);";
			System.out.println("sql="+sql);
			pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, sessionID);
			pstmt.setInt(2, Integer.parseInt(grade));
			pstmt.setString(3, suggest);

			pstmt.execute();

			

		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(pstmt!=null){
				try{
					pstmt.close();
				}catch(SQLException e){
					pstmt=null;
				}
			}
			if(conn!=null){
				try{
					conn.close();
				}catch(SQLException e){
					conn=null;
				}
				
			}
			
		}
		String path=request.getContextPath();
		response.sendRedirect(path+"/chat-ended.jsp");
		return;
		// response.getWriter().println("<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /><script type=\"text/javascript\">showTranscriptWindow(null);</script></head><body></body></html>");
		/*response.sendRedirect("transcriptmain.jsp?workgroup=" + workgroup
				+ "&chatID=" + chatID);*/
		/*String str=request.getContextPath();
		response.sendRedirect(str+"chat-ended.jsp");*/
	}

	public void init() throws ServletException {
		super.init();
	}
}